<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="${pageContext.request.contextPath}/js/jquery-1.8.3.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui/layui.js"></script>
</head>
<body>

<!-- 修改的form表单 star-->
<form class="layui-form" id="updateForm" style="display: none;padding: 10px 10px 0 0" lay-filter="updateFilter" method="post">
    <div class="layui-form-item">
        <label class="layui-form-label">图片名</label>
        <div class="layui-input-inline">
            <input type="text" name="bannerOldName" placeholder="请输入图片名" autocomplete="off" class="layui-input">
            <input type="hidden" name="bannerId">
        </div>
    </div>


    <!--《《《《《《《《《上传图片《《《《《《《《《》-->
    <div class="layui-form-item">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-block">
            <input type="hidden" name="bannerImageUrl" id="pic2">
            <button type="button" class="layui-btn" id="file2">
                <i class="layui-icon">&#xe67c;</i>点击上传
            </button>
            <img src="" id="bannerImageUrl2" width="100px" height="60px" style="display: none">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">是否显示</label>
        <div class="layui-input-block">
            <input type="radio" name="bannerState" value="0" title="显示" checked>
            <input type="radio" name="bannerState" value="1" title="不显示">
            <input type="radio" name="bannerState" value="2" title="待定">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">上传日期</label>
        <div class="layui-input-inline">
            <input id="bannerDate2" name="bannerDate" type="text" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">图片介绍</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入品牌故事" name="bannerDescription" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="update">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!--修改表单end-->

<!-- 添加的form表单 star-->
<form class="layui-form" id="addForm" style="display: none;padding: 10px 10px 0 0" method="post">
    <div class="layui-form-item">
        <label class="layui-form-label">图片名</label>
        <div class="layui-input-inline">
            <input type="text" name="bannerOldName" placeholder="请输入图片名" autocomplete="off" class="layui-input">
        </div>
    </div>


    <!--《《《《《《《《《上传图片《《《《《《《《《》-->
    <div class="layui-form-item">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-block">
            <input type="hidden" name="bannerImageUrl" id="pic1">
            <button type="button" class="layui-btn" id="file1">
                <i class="layui-icon">&#xe67c;</i>点击上传
            </button>
            <img src="" id="bannerImageUrl" width="100px" height="60px" style="display: none">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">是否显示</label>
        <div class="layui-input-block">
            <input type="radio" name="bannerState" value="0" title="显示" checked>
            <input type="radio" name="bannerState" value="1" title="不显示">
            <input type="radio" name="bannerState" value="2" title="待定">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">上传日期</label>
        <div class="layui-input-inline">
            <input id="bannerDate" name="bannerDate" type="text" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">图片介绍</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入品牌故事" name="bannerDescription" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!--添加表单end-->

<div>
    <button class="layui-btn" id="addBtn">添加</button>
</div>

<table id="demo" lay-filter="myTableFilter"></table>

<script type="text/html" id="toolbar1">
    <a class="layui-btn layui-btn-xs" lay-event="test1">修改</a>
    <a class="layui-btn layui-btn-xs" lay-event="test2">删除</a>
</script>

    <script>
        layui.use(['table','form','upload','laydate'], function() {

            var table = layui.table;
            var form = layui.form;
            var upload = layui.upload;
            var laydate = layui.laydate;
            laydate.render({
                elem:'#bannerDate',
                type:'datetime',
            })
            laydate.render({
                elem:'#bannerDate2',
                type:'datetime',
            })

            //渲染 table表格
            table.render({
                elem: '#demo'
                , url: '${pageContext.request.contextPath}/banner/selectPage' //数据接口
                , page: true //开启分页
                , limit: 5
                , limits: [3, 5, 10]
                , cols: [[ //表头
                      {field: 'bannerId', title: '编号'}
                    , {field: 'bannerOldName', title: '图片名'}
                    , {field: 'bannerState', title: '状态'}
                    , {field: 'bannerDate', title: '上传时间'}
                    , {field: 'bannerDescription', title: '图片介绍'}
                    , {field: 'bannerState', title: '是否显示'}
                    , {title: '图片', templet: function (d) {
                            return "<img src='"+"${pageContext.request.contextPath}/"+d.bannerImageUrl+"' width=\"100px\" height=\"60px\">";
                        }
                    }
                    , {title: '操作', toolbar: '#toolbar1'}
                ]]
            });

            //添加弹出框start
            $("#addBtn").click(function () {
                layer.open({
                    type:1,
                    content:$("#addForm"),
                    area:['500px','660px'],
                    title:'添加轮播图'
                })
            })
            //添加弹出框 end

            //logo图片上传 start
            upload.render({
                elem: '#file1'
                ,url: '${pageContext.request.contextPath}/banner/upload'
                ,done: function(res){ //上传后的回调
                    $("#pic1").val(res.pic);
                    $("#bannerImageUrl").prop("src","${pageContext.request.contextPath}/"+res.pic);
                    $("#bannerImageUrl").prop("style","");
                    layer.msg("图片上传成功");
                }
            })
            //图片上传 end

            //添加 start
            form.on("submit(*)",function() {
                var s = $("#addForm").serialize();
                $.ajax({
                    url:"${pageContext.request.contextPath}/banner/add",
                    data:s,
                    dataType:"json",
                    success:function(result){
                        if(result.flag){
                            layer.closeAll();
                            layer.msg("添加成功");
                            $("#addForm")[0].reset();
                            table.reload('demo',{
                                //从第几页开始
                                page:{curr:1}
                            })
                        }
                    }
                })
                return false;
            })
            //添加 end

            //删除修改start
            table.on('tool(myTableFilter)',function (obj) {
                 if(obj.event=='test1'){
                    layer.open({
                        type:1,
                        title:'修改品牌信息',
                        area:['500px','500px'],
                        content:$("#updateForm")
                    })
                    //回显
                    $.ajax({
                        url:'${pageContext.request.contextPath}/banner/selectOne',
                        data:'bannerId='+obj.data.bannerId,
                        dataType:'json',
                        success:function(result){
                            $("#bannerImageUrl2").prop("src","${pageContext.request.contextPath}/"+result.bannerImageUrl);
                            $("#bannerImageUrl2").prop("style","");
                            form.val('updateFilter',{
                                bannerId:result.bannerId,
                                bannerOldName:result.bannerOldName,
                                bannerState:result.bannerState,
                                bannerDate:result.bannerDate,
                                bannerDescription:result.bannerDescription,
                            })
                        }
                    })
                 }else
                // 删除
                if(obj.event=='test2'){
                    //询问弹出框
                    layer.confirm('确定删除吗?',function(index){
                        $.ajax({
                            url:'${pageContext.request.contextPath}/banner/delete',
                            data:'bannerId='+obj.data.bannerId,
                            dataType:'json',
                            success:function(result){
                                if(result.flag){
                                    layer.msg('删除成功');
                                    table.reload('demo',{
                                        //从第几页开始
                                        page:{curr:1}
                                    })
                                }
                            }
                        })
                    })
                }
            })
            //删除end

            //logo图片修改 start
            upload.render({
                elem: '#file2'
                ,url: '${pageContext.request.contextPath}/banner/upload'
                ,done: function(res){ //上传后的回调
                    $("#pic2").val(res.pic);
                    $("#bannerImageUrl2").prop("src","${pageContext.request.contextPath}/"+res.pic);
                    layer.msg("图片修改成功");
                }
            })
            //图片修改 end

            //修改  stear
            form.on("submit(update)",function() {
                var s = $("#updateForm").serialize();
                $.ajax({
                    url:"${pageContext.request.contextPath}/banner/update",
                    data:s,
                    dataType:"json",
                    success:function(result){
                        if(result.flag){
                            layer.closeAll();
                            layer.msg("修改成功");
                            table.reload('demo',{
                                //从第几页开始
                                page:{curr:1}
                            })
                        }
                    }
                })
                return false;
            })

        })
</script>
</body>
</html>
